# Contributor: Oleg A. Khlybov <fougas@mail.ru>

_realname=parmetis
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.0.3
pkgrel=2
pkgdesc="Parallel Graph Partitioning and Fill-reducing Matrix Ordering (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='http://glaros.dtc.umn.edu/gkhome/views/parmetis'
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-metis>=5.1.0" "${MINGW_PACKAGE_PREFIX}-msmpi")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-cc")
options=('strip' 'staticlibs')
source=("http://glaros.dtc.umn.edu/gkhome/fetch/sw/${_realname}/parmetis-${pkgver}.tar.gz"
        "0001-mingw-w64-does-not-have-sys-resource-h.patch"
        "0002-mingw-w64-do-not-use-reserved-double-underscored-names.patch"
        "0003-WIN32-Install-RUNTIME-to-bin.patch"
        "0004-METIS-GKLIB-location.patch"
        "0005-find-MPI.patch")
sha256sums=('f2d9a231b7cf97f1fee6e8c9663113ebf6c240d407d3c118c55b3633d6be6e5f'
            '22e62ec7dfbaf01865b1e09e7b3f3bad20188354efbef6e64d102a03c7bf748b'
            '141026e4e39f0529a916dce718273875ba12a98b0ebe7c2730e4950866ffc845'
            'b384fa51e01b9d3384fd067236e34036b0520f375d16abe575005e518dd5b1fd'
            'f1e7a174460ac79732de10d0f076f87b2ed60d185436bda0698bd012e70ea53c'
            '642f801967edc196e94e28f7fa3a63759ab33c1a534a5924d404acdcbfea2eb2')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  for p in ${srcdir}/*.patch; do
    patch -p1 -i "$p"
  done
}

build() {
  for _shared in OFF ON; do
    cd "$srcdir"/${_realname}-${pkgver}
    [[ -d "${srcdir}"/build-shared-${_shared}-${CARCH} ]] && rm -rf "${srcdir}"/build-shared-${_shared}-${CARCH}
    mkdir -p "${srcdir}"/build-shared-${_shared}-${CARCH} && cd "${srcdir}"/build-shared-${_shared}-${CARCH}
    declare -a extra_config
    if check_option "debug" "n"; then
      extra_config+=("-DCMAKE_BUILD_TYPE=Release")
    else
      extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
    fi
    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX="  \
      ${MINGW_PREFIX}/bin/cmake                    \
        -G'MSYS Makefiles'                         \
        -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX}     \
        -DCMAKE_C_COMPILER=mpicc                   \
        -DCMAKE_CXX_COMPILER=mpicxx                \
        -DCMAKE_C_FLAGS="${CPPFLAGS} ${CFLAGS}"    \
        "${extra_config[@]}"                       \
        -DSHARED=${_shared}                        \
        ../${_realname}-${pkgver}
    make
  done
}

package() {
  for _shared in OFF ON; do
    cd "${srcdir}"/build-shared-${_shared}-${CARCH}
    make install DESTDIR="${pkgdir}"
    mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
    echo "
      prefix=${MINGW_PREFIX}
      libdir=\${prefix}/lib
      includedir=\${prefix}/include
      Name: ${_realname}
      URL: ${url}
      Version: ${pkgver}
      Description: ${pkgdesc}
      Requires: metis msmpi
      Cflags: -I\${includedir}
      Libs: -L\${libdir} -l${_realname}
    " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc"
  done
  
  mv ${pkgdir}${MINGW_PREFIX}/bin/ptest{,_parmetis}.exe
  mv ${pkgdir}${MINGW_PREFIX}/bin/mtest{,_parmetis}.exe
}
